package autocode;

import com.celesea.framework.entity.BaseEntity;
import com.celesea.framework.util.StringKit;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;

public class CodeUtil {

    private static String dao = "";
    private static String service = "";
    private static boolean debug = false;

    public static void main(String[] args) throws Exception {
        // debug = true;
//        autoCode(Role.class, 1 | 2 | 4 | 8);
    }

    public enum FileType {
        Dao("dao", "", "Dao"), Service("service", "", "Service"), ServiceImpl("service", ".impl", "ServiceImpl");

        private String attr1;// 包名第一级替换
        private String attr2;// 包名第二级替换
        private String fileName;// 文件名后缀

        private FileType(String attr1, String attr2, String fileName) {
            this.attr1 = attr1;
            this.attr2 = attr2;
            this.fileName = fileName;
        }

        public String getAttr1() {
            return attr1;
        }

        public String getAttr2() {
            return attr2;
        }

        public String getFileName() {
            return fileName;
        }

        // com.lz.bus.%s.goods.%s
        // com.lz.bus.entity.goods.Brand
        // com.lz.bus.dao.goods.BrandDao
        // com.lz.bus.service.goods.BrandService
        // com.lz.bus.service.goods.impl.BrandServiceImpl
    }

    public static void autoCode(Class<? extends BaseEntity> clazz, int type) throws Exception {
        if ((type & 4) == 4) {
            autoDao(clazz, FileType.Dao);
        }
        if ((type & 1) == 1) {
            autoService(clazz, FileType.Service);
        }
        if ((type & 2) == 2) {
            autoServiceImpl(clazz, FileType.ServiceImpl);
        }
        if ((type & 8) == 8) {
            autoMapper(clazz);
        }
    }

    private static void autoMapper(Class<? extends BaseEntity> clazz) throws Exception {
        // com.lz.bus.entity.goods.Product
        initMapperFile(clazz);

        MapperUtil.renderMap(dao, clazz);

        closeFile();
    }

    private static void initMapperFile(Class<? extends BaseEntity> clazz) throws IOException, FileNotFoundException {
        String path = System.getProperty("user.dir") + ""+File.separator+"src"+File.separator+"main"+File.separator+"resources"+File.separator+"mapper";
        String packageName = clazz.getPackage().toString();
        packageName = packageName.replaceAll(""+File.separator+".", "/");
        // com/lz/bus/entity/goods/Product
        packageName = packageName.replaceAll("package com/lz", "");
        packageName = packageName.replaceAll("/entity", "");

        if (!debug) {
            File foleder = new File(path + packageName);
            if (!foleder.exists()) {
                foleder.mkdir();
            }
            String fileName = clazz.getSimpleName() + "Mapper.xml";
            File file = new File(foleder, fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            PrintStream printStream = new PrintStream(file);
            System.setOut(printStream);
        }
    }

    // 自动创建serviceImpl
    private static void autoServiceImpl(Class<? extends BaseEntity> clazz, FileType fileType) throws Exception {
        initFile(clazz, fileType);

        String packageName = clazz.getPackage().toString();
        packageName = packageName.replaceAll("entity", fileType.attr1);
        if (StringKit.isNotEmpty(fileType.attr2)) {
            packageName = packageName + fileType.attr2;
        }
        String fileName = clazz.getSimpleName() + fileType.fileName;

        System.out.println(packageName + ";");
        System.out.println();
        System.out.println("import org.springframework.beans.factory.annotation.Autowired;");
        System.out.println("import org.springframework.stereotype.Service;");
        System.out.println();
        System.out.println("import " + dao + ";");
        System.out.println("import " + clazz.getName() + ";");
        System.out.println("import " + service + ";");
        System.out.println("import AbstractBaseService;");
        System.out.println();
        System.out.println("@Service");
        System.out.println("public class " + fileName + " extends AbstractBaseService<" + clazz.getSimpleName() + "> implements " + clazz.getSimpleName() + "Service {");
        System.out.println();
        System.out.println("}");
        closeFile();
    }

    // 自动创建service
    private static void autoService(Class<? extends BaseEntity> clazz, FileType fileType) throws Exception {
        service = initFile(clazz, fileType);

        String packageName = clazz.getPackage().toString();
        packageName = packageName.replaceAll("entity", fileType.attr1);
        if (StringKit.isNotEmpty(fileType.attr2)) {
            packageName = packageName + fileType.attr2;
        }
        String fileName = clazz.getSimpleName() + fileType.fileName;

        System.out.println(packageName + ";");
        System.out.println();
        System.out.println("import " + clazz.getName() + ";");
        System.out.println("import BaseService;");
        System.out.println();
        System.out.println("public interface " + fileName + " extends BaseService<" + clazz.getSimpleName() + ">{");
        System.out.println();
        System.out.println("}");
        closeFile();
    }

    // 自动创建dao
    private static void autoDao(Class<? extends BaseEntity> clazz, FileType fileType) throws Exception {
        dao = initFile(clazz, fileType);

        String packageName = clazz.getPackage().toString();
        packageName = packageName.replaceAll("entity", fileType.attr1);
        if (StringKit.isNotEmpty(fileType.attr2)) {
            packageName = packageName + fileType.attr2;
        }
        String fileName = clazz.getSimpleName() + fileType.fileName;

        System.out.println(packageName + ";");
        System.out.println();
        System.out.println("import " + clazz.getName() + ";");
        System.out.println("import BaseDao;");
        System.out.println();
        System.out.println("public interface " + fileName + " extends BaseDao<" + clazz.getSimpleName() + ">{");
        System.out.println();
        System.out.println("}");
        closeFile();
    }

    private static String initFile(Class<? extends BaseEntity> clazz, FileType fileType) throws Exception {
        String path = System.getProperty("user.dir") + File.separator + "src"+ File.separator + "main"+File.separator+"java"+File.separator+"";
        String packageName = clazz.getName();
        packageName = packageName.substring(0, packageName.lastIndexOf("."));
        packageName = packageName.replaceAll("entity", fileType.attr1);
        if (StringKit.isNotEmpty(fileType.attr2)) {
            packageName = packageName + fileType.attr2;
        }
        String sign = packageName + "." + clazz.getSimpleName() + fileType.fileName;
        if (!debug) {
//            packageName = packageName.replaceAll("", File.separator);
            String fileName = clazz.getSimpleName() + fileType.fileName + ".java";
            File foleder = new File(path + packageName);
            if (!foleder.exists()) {
                foleder.mkdir();
            }
            File file = new File(foleder, fileName);

            if (!file.exists()) {
                file.createNewFile();
            }
            PrintStream printStream = new PrintStream(file);

            System.setOut(printStream);
        }
        return sign;
    }

    private static void closeFile() {
        if (!debug) {
            System.out.close();
        }
    }

}
